﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObjects;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.SqlServer
{
    public class SqlServerDieuKhoanHopDong: IDieuKhoanHopDong
    {
        public DieuKhoanHopDong GetDieuKhoanHopDong(int oid)
        {
            string sql = @" Select NgachLuong, BacLuong, HeSoLuong, Huong85PhanTramLuong
                            From DieuKhoanHopDong
                            Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", oid);
            DataRow row = DataProvider.GetDataRow(sql, param);

            DieuKhoanHopDong dieuKhoanHopDong = new DieuKhoanHopDong();

            dieuKhoanHopDong.Oid = oid;
            int ngachLuong;
            if (int.TryParse(row["NgachLuong"].ToString(), out ngachLuong))
            {
                SqlServerNgachLuong sqlServerNgachLuong = new SqlServerNgachLuong();
                dieuKhoanHopDong.NgachLuong = sqlServerNgachLuong.GetNgachLuong(ngachLuong);
            }
            int bacLuong;
            if (int.TryParse(row["BacLuong"].ToString(), out bacLuong))
            {
                SqlServerBacLuong sqlServerBacLuong = new SqlServerBacLuong();
                dieuKhoanHopDong.BacLuong = sqlServerBacLuong.GetBacLuong(bacLuong);
            }
            dieuKhoanHopDong.HeSoLuong = decimal.Parse(row["HeSoLuong"].ToString());
            dieuKhoanHopDong.Huong85PhanTramLuong = bool.Parse(row["Huong85PhanTramLuong"].ToString());

            return dieuKhoanHopDong;
        }
        /*
        public List<DieuKhoanHopDong> GetListDieuKhoanHopDong()
        {
            string sql = @" Select NgachLuong, BacLuong, HeSoLuong, Huong85PhanTramLuong
                            From DieuKhoanHopDong";

            DataTable dt = DataProvider.GetDataTable(sql);
            List<DieuKhoanHopDong> list = new List<DieuKhoanHopDong>();

            foreach (DataRow row in dt.Rows)
            {

                DieuKhoanHopDong dieuKhoanHopDong = new DieuKhoanHopDong();

                dieuKhoanHopDong.Oid = int.Parse(row["Oid"].ToString()) ;

                int ngachLuong;
                if (int.TryParse(row["NgachLuong"].ToString(), out ngachLuong))
                {
                    SqlServerNgachLuong sqlServerNgachLuong = new SqlServerNgachLuong();
                    dieuKhoanHopDong.NgachLuong = sqlServerNgachLuong.GetNgachLuong(ngachLuong);
                }
                int bacLuong;
                if (int.TryParse(row["BacLuong"].ToString(), out bacLuong))
                {
                    SqlServerBacLuong sqlServerBacLuong = new SqlServerBacLuong();
                    dieuKhoanHopDong.BacLuong = sqlServerBacLuong.GetBacLuong(bacLuong);
                }
                dieuKhoanHopDong.HeSoLuong = decimal.Parse(row["HeSoLuong"].ToString());
                dieuKhoanHopDong.Huong85PhanTramLuong = bool.Parse(row["Huong85PhanTramLuong"].ToString());

                list.Add(dieuKhoanHopDong);
            }
            return list;
        }
        */
        public int Insert(DieuKhoanHopDong dieuKhoanHopDong)
        {
            string sql = @" Insert into DieuKhoanHopDong(NgachLuong, BacLuong, HeSoLuong, Huong85PhanTramLuong)
                            Values(@NgachLuong, @BacLuong, @HeSoLuong, @Huong85PhanTramLuong)";

            SqlParameter[] param = {
                                       new SqlParameter("@NgachLuong", dieuKhoanHopDong.NgachLuong.Oid),
                                       new SqlParameter("@BacLuong", dieuKhoanHopDong.BacLuong.Oid),
                                       new SqlParameter("@HeSoLuong", dieuKhoanHopDong.HeSoLuong),
                                       new SqlParameter("@Huong85PhanTramLuong", dieuKhoanHopDong.Huong85PhanTramLuong)
                                   };
            int id = DataProvider.Insert(sql,true, param);
            if (id > 0)
                dieuKhoanHopDong.Oid = id;
            return id;
        }

        public int Update(DieuKhoanHopDong dieuKhoanHopDong)
        {
            string sql = @" Update DieuKhoanHopDong
                            Set NgachLuong = @Ngachluong,
                                BacLuong = @BacLuong,
                                HeSoLuong = @HeSoLuong,
                                Huong85PhanTramLuong = @Huong85PhanTramLuong
                            Where Oid = @Oid";
            SqlParameter[] param = {
                                       new SqlParameter("@Oid", dieuKhoanHopDong.Oid),
                                       new SqlParameter("@NgachLuong", dieuKhoanHopDong.NgachLuong.Oid),
                                       new SqlParameter("@BacLuong", dieuKhoanHopDong.BacLuong.Oid),
                                       new SqlParameter("@HeSoLuong", dieuKhoanHopDong.HeSoLuong),
                                       new SqlParameter("@Huong85PhanTramLuong", dieuKhoanHopDong.Huong85PhanTramLuong)
                                   };
            return DataProvider.Update(sql, param);
        }

        public int Delete(DieuKhoanHopDong dieuKhoanHopDong)
        {
            string sql = @" Delete From DieuKhoanHopDong Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", dieuKhoanHopDong.Oid);
            return DataProvider.Update(sql, param);
        }


        public int Save(DieuKhoanHopDong obj)
        {
            throw new NotImplementedException();
        }


    }
}
